package ca.usherbrooke.domus.sipnmove.useragent.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import ca.usherbrooke.domus.sipnmove.sip.SipFacade;
import ca.usherbrooke.domus.sipnmove.useragent.controller.ActionController;

/**
 * View implementation for the user agent
 */
@SuppressWarnings("serial")
public class UserAgentView extends JFrame implements WindowListener {

	/**
	 * Address text field
	 */
	private JTextField addressTextField;
	
	/**
	 * Call action controller
	 */
	private ActionController callActionController;

	/**
	 * Quit action controller
	 */
	private ActionController quitActionController;

	/**
	 * SIP facade reference
	 */
	private SipFacade sipFacade;
	
	/**
	 * Default constructor
	 * @param userAgent
	 */
	public UserAgentView(SipFacade sipFacade)
	{
		this.sipFacade = sipFacade;
	}
	
	/**
	 * Initialize the view
	 */
	public void display()
	{

		this.setSize(280, 200);
		this.setTitle("SIPnMove - User Agent");

		this.getContentPane().setLayout(new FlowLayout());

		addressTextField = new JTextField("sip:kilian@10.44.161.53:5070");
		addressTextField.setColumns(20);
		this.getContentPane().add(addressTextField);

		CallButton callButton = new CallButton(callActionController);
		this.getContentPane().add(callButton);

		QuitButton quitButton = new QuitButton(quitActionController);
		this.getContentPane().add(quitButton);

		JLabel labelIp = new JLabel("IP:       " + sipFacade.getSipUserInformation().getIpAddress());
		labelIp.setPreferredSize(new Dimension(250, 20));
		this.getContentPane().add(labelIp);
		
		JLabel labelPort = new JLabel("Port:     " + sipFacade.getSipUserInformation().getPort());
		labelPort.setPreferredSize(new Dimension(250, 20));
		this.getContentPane().add(labelPort);
		
		JLabel labelUsername = new JLabel("Username: " + sipFacade.getSipUserInformation().getName());
		labelUsername.setPreferredSize(new Dimension(250, 20));
		this.getContentPane().add(labelUsername);
		
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(this);
		
		this.setVisible(true);

	}
	
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		quitActionController.doAction();
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void setCallActionController(ActionController callActionController) {
		this.callActionController = callActionController;
	}

	public void setQuitActionController(ActionController quitActionController) {
		this.quitActionController = quitActionController;
	}

	public JTextField getAddressTextField() {
		return this.addressTextField;
	}

}
